package com.csht.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.csht.model.City;
import com.csht.model.CshtUser;
import com.csht.model.Province;
import com.csht.model.School;
import com.csht.service.MyService;
import com.csht.util.MD5;
import com.csht.vo.index.user.CshtUserVo;

/**
 * 我的资料
 * @author chenhuiyan
 *
 */
@Controller
@RequestMapping(value = "/toMy")
public class MyController {
	
	/**
	 * 获取省名称链接
	 */
	private String provinceUrl = "http://119.29.166.254:9090/api/provinces";
	
	/**
	 * 通过省id查找城市
	 */
	private String cityUrl = "http://119.29.166.254:9090/api/province/getCitiesByProvinceId";
	
	/**
	 * 通过城市名获取学校
	 */
	private String schoolUrl = "http://119.29.166.254:9090/api/university/getUniversityByCityName";
	
	@Autowired
	private MyService myService;
	
	/**
	 * 个人中心－我的资料
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/my")
	public String my(ModelMap model, HttpServletRequest request) throws Exception{
		
		return "/toMy/my";
	}
	
	/**
	 * 修改头像
	 * @param multipartFile
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/updateUserHeadById")
	public Map<String, Object> updateUserHeadById(@RequestParam(name = "updateUserHead") MultipartFile multipartFile, 
			HttpServletRequest request) throws Exception{
		String riginalFilename = multipartFile.getOriginalFilename(); //获取原文件名
		String path = "D:/Tomcat/csht_file/user_header/"; //上传文件路径
		String fileSuffixName = riginalFilename.substring(riginalFilename.lastIndexOf(".") + 1); //截取后缀名
		String fileName = System.currentTimeMillis() + "." + fileSuffixName; //文件名
		File file = new File(path, fileName); //创建文件
		multipartFile.transferTo(file); //保存文件
		CshtUser cshtUser = (CshtUser) request.getSession().getAttribute("loginUser"); //获取用户
		CshtUserVo cshtUserVo = (CshtUserVo) request.getSession().getAttribute("cshtUserVo"); //获取封装的用户
		String riginalFileHead = cshtUser.getUserHeadPath(); //获取到用户原来的头像名
		
		if(!"avater.jpg".equals(riginalFileHead)) { //判断是否为系统默认头像，是就不删除，不是则删除
			File deleteFile = new File(path, riginalFileHead); //创建要删除的文件
			deleteFile.delete(); //删除原来头像
		}
		
		cshtUser.setUserHeadPath(fileName); //更新头像地址
		cshtUserVo.setUserHeadPath(fileName); //更新头像地址
		request.getSession().setAttribute("loginUser", cshtUser); //更新session attribute的用户信息
		request.getSession().setAttribute("cshtUserVo", cshtUserVo); //更新session attribute的用户信息
		//更新数据库用户头像地址
		Map<String, String> fileNameAndUserId = new HashMap<String, String>();
		fileNameAndUserId.put("fileName", fileName);
		fileNameAndUserId.put("userId", cshtUser.getUserId().toString());
		myService.updateUserHeadById(fileNameAndUserId);
		
		//返回值(按照layui上传文件的回调格式)
		Map<String, Object> messages = new HashMap<String, Object>();
		messages.put("code", "0");
		
		return messages;
	}
	
	/**
	 * 根据用户id修改用户名
	 * @param userUsername
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/updateUsernameById")
	public Map<String, Object> updateUsernameById(@RequestParam(name = "username") String userUsername, 
			HttpServletRequest request) throws Exception{
		Map<String, Object> messages = new HashMap<String, Object>();
		messages.put("message", false);
		CshtUser cshtUser = (CshtUser) request.getSession().getAttribute("loginUser"); //获取登录用户
		CshtUserVo cshtUserVo = (CshtUserVo) request.getSession().getAttribute("cshtUserVo"); //获取登录用户的包装类
		Map<String, String> userIdAndUsername = new HashMap<String, String>();
		userIdAndUsername.put("userId", cshtUser.getUserId().toString());
		userIdAndUsername.put("userUsername", userUsername);
		myService.updateUsernameById(userIdAndUsername); //更新数据库
		cshtUser.setUserUsername(userUsername); 
		cshtUserVo.setUserUsername(userUsername); 
		request.getSession().setAttribute("loginUser", cshtUser); //更新session attribute的用户信息
		request.getSession().setAttribute("cshtUserVo", cshtUserVo); //更新session attribute的用户信息
		messages.put("message", true);
		return messages;
	}
	
	/**
	 * 根据用户id修改密码
	 * @param userPassword
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/updateUserPasswordById")
	public Map<String, Object> updateUserPasswordById(@RequestParam(name = "userPassword") String userPassword, 
			HttpServletRequest request) throws Exception{
		Map<String, Object> messages = new HashMap<String, Object>();
		messages.put("res", false);
		CshtUser cshtUser = (CshtUser) request.getSession().getAttribute("loginUser"); //获取登录用户
		Map<String, String> userIdAndUserPassword = new HashMap<String, String>(); //封装参数
		userIdAndUserPassword.put("userId", cshtUser.getUserId().toString());
		userIdAndUserPassword.put("userPassword", MD5.getMD5Code(userPassword));
		myService.updateUserPasswordById(userIdAndUserPassword); //更新数据库
		messages.put("res", true);
		//修改密码成功，则需要重新登录,清除session
		request.getSession().removeAttribute("loginUser");
		request.getSession().removeAttribute("cshtUserVo");
		return messages;
	}
	
	/**
	 * 获取省份
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/toUpdategetAllProvice")
	public Map<String, List<Province>> toUpdategetAllProvice() throws Exception {
		List<Province> provinceList = myService.toUpdategetAllProvice(provinceUrl);
		Map<String, List<Province>> messages = new HashMap<String, List<Province>>();
		messages.put("provinceList", provinceList);
		return messages;
	}
	
	/**
	 * 根据省ID获取城市
	 * @param provinceId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/toUpdategetAllCity")
	public Map<String, List<City>> toUpdategetAllCity(@RequestParam(name = "provinceId") Integer provinceId) 
			throws Exception{
		List<City> cityList = myService.toUpdategetAllCity(cityUrl, provinceId);
		Map<String, List<City>> messages = new HashMap<String, List<City>>();
		messages.put("cityList", cityList);
		return messages;
	}
	
	/**
	 * 根据学校ID获取学校
	 * @param cityName
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/toUpdategetAllSchool")
	public Map<String, List<School>> toUpdategetAllSchool(@RequestParam(name = "cityName") String cityName) 
			throws Exception{
		List<School> schoolList = myService.toUpdategetAllSchool(schoolUrl, cityName);
		Map<String, List<School>> messages = new HashMap<String, List<School>>();
		messages.put("schoolList", schoolList);
		return messages;
	}
	
	/**
	 * 根据用户id修改地址
	 * @param address
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/updateUserAddressById")
	public Map<String, Object> updateUserAddressById(@RequestParam(name = "address") String address, 
			HttpServletRequest request) throws Exception{
		CshtUser cshtUser = (CshtUser) request.getSession().getAttribute("loginUser"); //获取登录用户
		CshtUserVo cshtUserVo = (CshtUserVo) request.getSession().getAttribute("cshtUserVo"); //获取登录用户的包装类
		Map<String, Object> messages = new HashMap<String, Object>();
		messages.put("res", false);
		Map<String, String> userIdAndAddress = new HashMap<String, String>();
		userIdAndAddress.put("userId", cshtUser.getUserId().toString());
		userIdAndAddress.put("userAddress", address);
		myService.updateUserAddressById(userIdAndAddress);//更新数据库
		messages.put("res", true);
		cshtUser.setUserAddress(address);
		cshtUserVo.setUserAddress(address);
		request.getSession().setAttribute("loginUser", cshtUser);//更新session attribute的用户信息
		request.getSession().setAttribute("cshtUserVo", cshtUserVo);//更新session attribute的用户信息
		return messages;
	}
	
	/**
	 * 根据用户id修改用户性别
	 * @param userGender
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/updateUserGenderById")
	public Map<String, Object> updateUserGenderById(@RequestParam(name = "userGender") String userGender, 
			HttpServletRequest request) throws Exception{
		CshtUser cshtUser = (CshtUser) request.getSession().getAttribute("loginUser"); //获取登录用户
		CshtUserVo cshtUserVo = (CshtUserVo) request.getSession().getAttribute("cshtUserVo"); //获取登录用户的包装类
		Map<String, Object> messages = new HashMap<String, Object>();
		messages.put("res", false);
		Map<String, String> userIdAndGender = new HashMap<String, String>();
		userIdAndGender.put("userId", cshtUser.getUserId().toString());
		userIdAndGender.put("userGender", userGender);
		myService.updateUserGenderById(userIdAndGender);//更新数据库
		messages.put("res", true);
		cshtUser.setUserGender(userGender);
		cshtUserVo.setUserGender(userGender);
		request.getSession().setAttribute("loginUser", cshtUser);//更新session attribute的用户信息
		request.getSession().setAttribute("cshtUserVo", cshtUserVo);//更新session attribute的用户信息
		return messages;
	}
	
	/**
	 * 根据用户id修改QQ号
	 * @param userqq
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/updateUserqqById")
	public Map<String, Object> updateUserqqById(@RequestParam(name = "userqq") String userqq, 
			HttpServletRequest request) throws Exception{
		CshtUser cshtUser = (CshtUser) request.getSession().getAttribute("loginUser"); //获取登录用户
		CshtUserVo cshtUserVo = (CshtUserVo) request.getSession().getAttribute("cshtUserVo"); //获取登录用户的包装类
		Map<String, Object> messages = new HashMap<String, Object>();
		messages.put("res", false);
		Map<String, String> userIdAnduserqq = new HashMap<String, String>();
		userIdAnduserqq.put("userId", cshtUser.getUserId().toString());
		userIdAnduserqq.put("userqq", userqq);
		myService.updateUserqqById(userIdAnduserqq);//更新数据库
		messages.put("res", true);
		cshtUser.setuserqq(userqq);
		cshtUserVo.setuserqq(userqq);
		request.getSession().setAttribute("loginUser", cshtUser);//更新session attribute的用户信息
		request.getSession().setAttribute("cshtUserVo", cshtUserVo);//更新session attribute的用户信息
		return messages;
	}
	
	/**
	 * 根据用户id修改微信号
	 * @param userWechat
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/updateUserWechatById")
	public Map<String, Object> updateUserWechatById(@RequestParam(name = "userWechat") String userWechat, 
			HttpServletRequest request) throws Exception{
		CshtUser cshtUser = (CshtUser) request.getSession().getAttribute("loginUser"); //获取登录用户
		CshtUserVo cshtUserVo = (CshtUserVo) request.getSession().getAttribute("cshtUserVo"); //获取登录用户的包装类
		Map<String, Object> messages = new HashMap<String, Object>();
		messages.put("res", false);
		Map<String, String> userIdAndUserWechat = new HashMap<String, String>();
		userIdAndUserWechat.put("userId", cshtUser.getUserId().toString());
		userIdAndUserWechat.put("userWechat", userWechat);
		myService.updateUserWechatById(userIdAndUserWechat);//更新数据库
		messages.put("res", true);
		cshtUser.setUserWechat(userWechat);
		cshtUserVo.setUserWechat(userWechat);
		request.getSession().setAttribute("loginUser", cshtUser);//更新session attribute的用户信息
		request.getSession().setAttribute("cshtUserVo", cshtUserVo);//更新session attribute的用户信息
		return messages;
	}
	
}
